package com.sijia.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sijia.reggie.comment.BaseContext;
import com.sijia.reggie.comment.R;
import com.sijia.reggie.entity.ShoppingCart;
import com.sijia.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 杨思佳
 * @version 1.0
 */

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService scs;


    /**
     * 根据用户id获取购物车信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        Long user = (long)session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, user);
        List<ShoppingCart> shoppingCarts = scs.list(queryWrapper);
        return R.success(shoppingCarts);

    }


    /**
     * 将菜品或套餐加入购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        shoppingCart.setUserId(BaseContext.getCurrentId());
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = scs.getOne(queryWrapper);
        if (one == null){
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            scs.save(shoppingCart);
        } else {
            Integer number = one.getNumber();
            one.setNumber(++number);
            scs.updateById(one);
        }
        return R.success("");
    }

    /**
     * 删除单个菜品或套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<String> delete(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if (shoppingCart.getDishId() != null){
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = scs.getOne(queryWrapper);
        if (cart.getNumber() <= 1){
            scs.remove(queryWrapper);
        } else {
            cart.setNumber(cart.getNumber() - 1);
            scs.updateById(cart);
        }
        return R.success("删除成功");
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        scs.clean();
        return R.success("购物车清空成功");
    }
}
